/*
 * @Author: biankai
 * @Date: 2021-11-04 14:27:50
 * @LastEditTime: 2021-11-10 09:08:38
 * @LastEditors: biankai
 * @FilePath: /gitee/devops_service/src/services/kubernetes/deployment.service.ts
 */
import {
    Injectable,
    Inject,
    HttpService,
    BadRequestException,
} from '@nestjs/common';
import * as kubernetes from '@kubernetes/client-node';
import { BaseService } from './base.service';
@Injectable()
export class DeploymentService extends BaseService {
    private readonly k8sApi: kubernetes.AppsV1Api;

    constructor(@Inject(HttpService) private readonly httpService: HttpService) {
        super()
        const kc = new kubernetes.KubeConfig();
        kc.loadFromFile(this.k8sConfig)
        this.k8sApi = kc.makeApiClient(kubernetes.AppsV1Api);
    }
    async listNamespacedDeployment(namespaceName) {
        const resBody = await this.k8sApi.listNamespacedDeployment(namespaceName);
        return await resBody.body;
    }
    async readNamespacedDeployment(namespaceName, name) {
        const resBody = await this.k8sApi.readNamespacedDeployment(name, namespaceName);
        return await resBody.body;
    }
    async createNamespacedDeployment(namespaceName, deployment) {
        var options = {
            "apiVersion": "apps/v1",
            "kind": "Deployment",
            "metadata": {
                "labels": {
                    "app": deployment.name
                },
                "name": deployment.name
            },
            "spec": {
                "replicas": 1,
                "selector": {
                    "matchLabels": {
                        "app": deployment.name
                    }
                },
                "template": {
                    "metadata": {
                        "labels": {
                            "app": deployment.name
                        }
                    },
                    "spec": {
                        "containers": [{
                            // "image": "200.200.200.80:8082/web-dev/devops-saas-admin:7018df2",
                            "image": deployment.image,
                            "name": deployment.name,
                            "ports": [{
                                "containerPort": 80
                            }]
                        }]
                    }
                }
            }
        }
        const resBody = await this.k8sApi.createNamespacedDeployment(namespaceName, options);
        return await resBody.body;
    }
    async replaceNamespacedDeployment(namespaceName, name, deployment) {
        var options = {
            "apiVersion": "apps/v1",
            "kind": "Deployment",
            "metadata": {
                "labels": {
                    "app": deployment.name
                },
                "name": deployment.name
            },
            "spec": {
                "replicas": 1,
                "selector": {
                    "matchLabels": {
                        "app": deployment.name
                    }
                },
                "template": {
                    "metadata": {
                        "labels": {
                            "app": deployment.name
                        }
                    },
                    "spec": {
                        "containers": [{
                            // "image": "200.200.200.80:8082/web-dev/devops-saas-admin:7018df2",
                            "image": deployment.image,
                            "name": deployment.name,
                            "ports": [{
                                "containerPort": 80
                            }]
                        }]
                    }
                }
            }
        }
        const resBody = await this.k8sApi.replaceNamespacedDeployment(name, namespaceName, options);
        return await resBody.body;
    }
    async deleteNamespacedDeployment(namespaceName, name) {
        const resBody = await this.k8sApi.deleteNamespacedDeployment(name, namespaceName);
        return await resBody.body;
    }
}
